//
//  StudentAddViewController.m
//  UI11_Address
//
//  Created by dllo on 15/10/24.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "StudentAddViewController.h"
#import "StudentAddView.h"
#import "Students.h"
#import "ChineseToPinyin.h"
#import "Singleton.h"
@interface StudentAddViewController ()<StudentAddViewDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>

@property (nonatomic, retain)StudentAddView *stuAddV;

@end

@implementation StudentAddViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.stuAddV = [[StudentAddView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    self.stuAddV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.stuAddV];
    [_stuAddV release];
    
   self.stuAddV.studentAddDelegate = self;


#pragma mark -- 创建右侧按钮

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonAction:)];
}


#pragma mark -- 协议方法

-(void)presentToViewController{
    
    //创建相册
    UIImagePickerController *pic = [[UIImagePickerController alloc]init];
    pic.allowsEditing =YES;
    pic.delegate = self;
    [self presentViewController:pic animated:YES completion:^{
        
    }];
    
}
#pragma mark -- 相册方法

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    NSLog(@"info == %@",info);
    
    
    //设置头像可以在相册里选择
    self.stuAddV.headImageView.image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
    
    //设置背景与头像相同
    self.stuAddV.backGroundImageView.image = self.stuAddV.headImageView.image;
    
    [picker dismissViewControllerAnimated:YES completion:^{
        
        
    }];
    
}


#pragma mark -- 右键按钮方法
-(void)rightButtonAction:(UIBarButtonItem *)button
{
    
    //新建个学生对象
    Students *dvStu = [[Students alloc]init];
    
    //把修改的值传给这个学生对象
    dvStu.name = self.stuAddV.nameField.text;
    dvStu.phoneNumber = self.stuAddV.phoneField.text;
    dvStu.gender= self.stuAddV.genderField.text;
    dvStu.myimage = self.stuAddV.headImageView.image;
    
    //取出新学生姓名的首字母
    char s = [ChineseToPinyin sortSectionTitle:dvStu.name];
    NSString *newKey = [NSString stringWithFormat:@"%c",s];
    //        NSLog(@"----%@",  newKey);
    
    
    //如果单例的key数组中包含 新学生姓名的首字母
    if ([[Singleton shareSingleton].allKeysArray containsObject:newKey]) {
        
        //在单例的字典中找到 新学生姓名的首字母 的数组
        NSMutableArray *array = [[Singleton shareSingleton].studentDic objectForKey:newKey];
        
        //把学生加入到这个数组中
        [array addObject:dvStu];
        
        
    }else{
        //新创建个数组
        NSMutableArray *array = [NSMutableArray array];
        
        //在单例的字典中 加入以新学生姓名的首字母为key 的数组
        [[Singleton shareSingleton].studentDic setObject:array forKey:newKey];
        
        //将新学生加入到数组中
        [array addObject:dvStu];
        
        
        //将新key加入到 单例key数组中
        [[Singleton shareSingleton].allKeysArray addObject:newKey];
        
        //排序
        [[Singleton shareSingleton].allKeysArray sortUsingSelector:@selector(compare:)];
        
        
    }
    [self.stuAddVCDeleagte presentToViewController];
    [self.navigationController popToRootViewControllerAnimated:YES];

}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
